package com.awenhui.demo.temp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;


/**
 * 智能门锁接口
 */
interface SmartLock {
    String getPassword();
}

/**
 * 中移智能门锁
 */
@Component
class CmccSmartLock implements SmartLock {

    @Override
    public String getPassword() {
        return "123456";
    }
}

/**
 * 海尔智能门锁
 */
@Component
class HaierSmartLock implements SmartLock {

    @Override
    public String getPassword() {
        return "654895";
    }
}

/**
 * 门锁工厂类
 */
@Component
class SmartLockFactory {

    @Autowired
    ApplicationContext applicationContext;

    /**
     * 获取门锁（不同门锁编码规则不一样）
     *
     * @param lockNo 门锁编号
     * @return
     */
    public SmartLock getSmartLock(String lockNo) {
        if (lockNo.contains("."))return applicationContext.getBean(CmccSmartLock.class);
        if (lockNo.contains("-"))return   applicationContext.getBean(HaierSmartLock.class);
        return null;
    }

}

@Service
public class HouseLiveSevice {

    @Autowired
    SmartLockFactory smartLockFactory;

    public void commitLive(String lockNo, String renter) {
        String password = smartLockFactory.getSmartLock(lockNo).getPassword();
        System.out.println(renter + "入住成功，开门密码为：" + password);
    }
}




